﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace MyApi.Hubs
{

    public class DiscussHub : Hub
    {
        private static readonly Dictionary<string, object> ConnectClientId = new Dictionary<string, object>();

        /// <summary>
        /// 所有人接收
        /// </summary>
        /// <param name="callmethod">调用的js方法名称</param>
        /// <param name="val"></param>
        /// <returns></returns>
        public async Task ToAll(string callmethod, string val)
        {
            await Clients.All.InvokeAsync(callmethod, Context.ConnectionId + "返回值=" + val);
        }

        /// <summary>
        /// 当前id接收
        /// </summary>
        /// <param name="callmethod">调用的js方法名称</param>
        /// <param name="connectionid"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public async Task ToClient(string callmethod, string connectionid, string val)
        {
            await Clients.Client(connectionid).InvokeAsync(callmethod, connectionid + "返回值=" + val);
        }

        /// <summary>
        /// 发送和接收
        /// </summary>
        /// <param name="callmethod">调用的js方法名称</param>
        /// <param name="sendid">发送id</param>
        /// <param name="receiveid">接收id</param>
        /// <param name="val">值</param>
        /// <returns></returns>
        public async Task ClientToClient(string callmethod, string sendid, string receiveid, string val)
        {
            await Clients.Client(receiveid).InvokeAsync(callmethod, "从" + sendid + "发给" + receiveid + "返回值=" + val);
        }

        /// <summary>
        /// 连接成功，添加ConnectClientId到列表
        /// </summary>
        /// <param name="connectionid"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public void AddConnectClientId(string connectionid, object val)
        {
            ConnectClientId.Add(connectionid, val);
        }
    }
}
